#!/bin/bash
#----------------------------------------------------------------------
# Build a source tarball for wxWidgets and wxPython

#set -o xtrace
##set -o errexit

spectemplate=distrib/wxPythonFull.spec.in

if [ ! -d wx/lib -o ! -e ${spectemplate} ]; then
    echo "Please run this script from the root wxPython directory."
    exit 1
fi


#----------------------------------------------------------------------
# Initialization

version=`python -c "import setup;print setup.VERSION"`
wxpdir=`pwd`
if [ "$WXWIN" != "" ]; then
    wxdir=$WXWIN
else
    wxdir=${wxpdir}/../wxWidgets
fi

distdir=${wxpdir}/dist
builddir=${wxpdir}/_build_

pythonbin=/usr/bin/python
tarname=wxPython-src

pyver=2.6
ver2=`echo ${version} | cut -c 1,2,3`
tarver=${tarname}-${version}
pythonbin=/usr/bin/python
python=${pythonbin}${pyver}
if [ ! -e ${python} ]; then
    echo "${python} not found!"
    exit 1
fi


function makespec {
    echo "*** Writing ${distdir}/wxPython.spec"
    cat ${spectemplate} \
	| sed s:@PYTHON@:${python}:g \
	| sed s:@PYVER@:${pyver}:g \
	| sed s:@TARNAME@:${tarname}:g \
	| sed s:@VERSION@:${version}:g \
	| sed s:@VER2@:${ver2}:g \
	> ${distdir}/wxPython.spec
}


#----------------------------------------------------------------------
# Copy the sources from the local workspace

function cleanup {
    RMFILES=`find . -name "$1"`
    if [ "$RMFILES" != "" ]; then
	rm -rf $RMFILES
    fi
}


echo "*** Copying workspace tree"
mkdir -p ${builddir}
mkdir -p ${distdir}
pushd ${builddir} > /dev/null
if [ -e ${tarver} ]; then
    rm -rf ${tarver}
fi
mkdir -p ${tarver}

# copy root dir contents
cp -pf --link ${wxdir}/* ${tarver} > /dev/null 2>&1

# copy some specific top level dirs
for d in art build debian demos docs include interface lib \
         locale patches samples src utils; do
    if [ -e ${wxdir}/$d ]; then
	cp -Rpf --link ${wxdir}/$d ${tarver} #> /dev/null 2>&1
    fi
done
mkdir ${tarver}/distrib
cp -Rpf --link ${wxdir}/distrib/scripts ${tarver}/distrib


# now do the same thing for wxPython, but use the DIRLIST to select dirs to copy
for dir in `grep -v '#' ${wxpdir}/distrib/DIRLIST`; do
    mkdir -p ${tarver}/wxPython/${dir}
    cp -pf --link ${wxpdir}/${dir}/* ${tarver}/wxPython/${dir} > /dev/null 2>&1
done

# using DIRLIST as above will normally skip any files starting
# with a dot, but there are a few .files that we do want to
# copy...
cp -pf --link ${wxpdir}/distrib/msw/.[a-zA-Z]* ${tarver}/wxPython/distrib/msw  > /dev/null 2>&1

# cleanup
echo "*** Removing uneeded stuff from copy of workspace tree"
pushd ${tarver} > /dev/null
cleanup .cvsignore
cleanup CVS
cleanup CVSROOT
cleanup .svn
rm BuildCVS.txt
rm -f ChangeLog
rm *.spec
rm -rf distrib/msw/tmake
rm locale/*.mo
cleanup ".#*"
cleanup "#*#"
cleanup "*~"
cleanup "*.orig"
cleanup "*.rej"
cleanup "*.pyc"
##cleanup core  # pubsub has a "core" subpackage
cleanup "core.[0-9]*"
rm -f wxPython/samples/embedded/embedded
rm -f wxPython/samples/embedded/embedded.o

# ports that are not supported yet
cleanup mgl
cleanup motif
cleanup os2
##cleanup x11
cleanup univ
cleanup wine

rm -f wxPython/wx/*  > /dev/null 2>&1

popd > /dev/null
popd > /dev/null



#----------------------------------------------------------------------
# Make the spec file and copy to ${builddir}/${tarver} so it will be
# in the tar file when it's built

makespec
cp ${distdir}/wxPython.spec ${builddir}/${tarver}/wxPython.spec


#----------------------------------------------------------------------
# Build the tar file

echo "*** Creating language catalogs..."
pushd ${builddir}/${tarver}/locale > /dev/null
make allmo
popd > /dev/null

echo "*** Creating tarball..."
cp distrib/README.1st.txt ${builddir}/${tarver}
pushd ${builddir} > /dev/null
tar cf ${distdir}/${tarver}.tar ${tarver} > /dev/null

#ext=gz
#cmd="gzip"
ext=bz2
cmd="bzip2 --best"

echo "*** Compressing..."
if [ -e ${distdir}/${tarver}.tar.$ext ]; then
    rm ${distdir}/${tarver}.tar.$ext
fi
$cmd ${distdir}/${tarver}.tar

popd > /dev/null


#----------------------------------------------------------------------
# Make the SRPM 

echo "*** Building SRPM..."

rpmtop=${builddir}/rpmtop
for dir in SOURCES SPECS BUILD RPMS SRPMS; do
    if [ ! -d ${rpmtop}/${dir} ]; then
	mkdir -p ${rpmtop}/${dir}
    fi
done

cp ${distdir}/${tarver}.tar.bz2 ${rpmtop}/SOURCES
	rpmbuild -bs \
	    --define "_topdir ${rpmtop}" \
	    --define "_tmppath ${builddir}" \
	    --define "release 1" \
	    ${distdir}/wxPython.spec

echo "*** Moving RPM to ${distdir}"
mv -f `find ${rpmtop} -name "wxPython*.rpm"` ${distdir}


#----------------------------------------------------------------------
# Make another tarball with only the things needed for the Debian build

echo "*** Making trimmed tarball for Debian builds"
pushd ${builddir} > /dev/null

mv ${tarver} wxwidgets${ver2}-${version}

# remove some of the platform specific dirs
for dir in msw msdos msvc mac cocoa osx dfb palmos; do
    find . -name ${dir} -type d -print | xargs rm -rf
done

tar cf ${distdir}/wxwidgets${ver2}_${version}.orig.tar wxwidgets${ver2}-${version}
gzip -9 ${distdir}/wxwidgets${ver2}_${version}.orig.tar

popd > /dev/null

#----------------------------------------------------------------------
# Cleanup

echo "*** Cleaning up"
rm -rf ${rpmtop}
rm -rf ${builddir}




